package io.renren.modules.store.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.common.utils.BeanJudgeUtil;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TStoreRequirementEntity;
import io.renren.modules.store.service.TStoreRequirementService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-25 02:05:20
 */
@RestController
@RequestMapping("store/tstorerequirement")
public class TStoreRequirementController extends AbstractController {
    @Autowired
    private TStoreRequirementService tStoreRequirementService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstorerequirement:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        params.put("key",params.get("key"));
        PageUtils page = tStoreRequirementService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{requirementId}")
    @RequiresPermissions("store:tstorerequirement:info")
    public R info(@PathVariable("requirementId") Long requirementId){
		TStoreRequirementEntity tStoreRequirement = tStoreRequirementService.getById(requirementId);

        return R.ok().put("tStoreRequirement", tStoreRequirement);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstorerequirement:save")
    public R save(@RequestBody TStoreRequirementEntity tStoreRequirement){
        TStoreRequirementEntity one = tStoreRequirementService.getOne(new QueryWrapper<TStoreRequirementEntity>()
                .eq("seller_id", getSeller())
                .eq("requirement_name", tStoreRequirement.getRequirementName())
        );
        if (!BeanJudgeUtil.beanIsNull(one)){
            throw new RRException("要求已存在,不能重复添加");
        }
        tStoreRequirement.setSellerId(getSeller());
		tStoreRequirementService.save(tStoreRequirement);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstorerequirement:update")
    public R update(@RequestBody TStoreRequirementEntity tStoreRequirement){
		tStoreRequirementService.updateById(tStoreRequirement);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstorerequirement:delete")
    public R delete(@RequestBody Long[] requirementIds){
		tStoreRequirementService.removeByIds(Arrays.asList(requirementIds));

        return R.ok();
    }

}
